package team.seekers.oj.third.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.ApiException;
import team.seekers.oj.third.service.VodService;
import team.seekers.oj.third.utils.ConstantVodUtils;
import team.seekers.oj.third.utils.InitObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Godc
 * @description
 */
@RestController
@RequestMapping("/eduVod/videoOss")
@CrossOrigin
public class VodController {


    @Autowired
    private VodService vodService;


    @PostMapping(value = "/uploadVideo")
    @ApiResponseBody
    public String uploadVideo(MultipartFile file){
       String videoId = vodService.uploadVideo(file);
       return videoId;
    }


    //删除多个阿里云视频
    @DeleteMapping("/removeBatch")
    @ApiResponseBody
    public R deleteBatch(@RequestParam("videoList") List<String> videoList){
        vodService.removeBatchAlyVideo(videoList);
        return null;
    }

    @GetMapping("/getPlayUrl/{videoId}")
    @ApiResponseBody
    public List<String> getPlayUrl(@PathVariable("videoId") String videoId) throws ClientException {
        //1.创建初始化对象
        DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.KEY_ID,ConstantVodUtils.KEY_SECRET);
        //2.创建获取视频地址request和response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        //3.向request对象里面设置视频id
        request.setVideoId(videoId);
        //4.调用初始化对象里面的方法，传递request，获取数据
        response = client.getAcsResponse(request);

        ArrayList<String> videoUrlList = new ArrayList<>();

        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            videoUrlList.add(playInfo.getPlayURL());
        }
//        //Base信息
//        System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");

        return videoUrlList;
    }



}
